package cn.flyme.shop.controller;

import cn.flyme.shop.entities.Cata;
import cn.flyme.shop.entities.Customer;
import cn.flyme.shop.entities.Orders;
import cn.flyme.shop.service.ShopService;
import cn.flyme.shop.service.SupermarketService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
public class Supermarket {

    @Autowired
    private SupermarketService supermarketService;

    @Autowired
    private ShopService shopService;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "index";
    }
    @RequestMapping(value = "/index-02",method = RequestMethod.GET)
    public String index02(){
        return "index-02";
    }
    @RequestMapping(value = "/index-03",method = RequestMethod.GET)
    public String index03(){
        return "index-03";
    }
    @RequestMapping(value = "/index-04",method = RequestMethod.GET)
    public String index04(){
        return "index-04";
    }
    @RequestMapping(value = "/index-05",method = RequestMethod.GET)
    public String index05(){
        return "index-05";
    }
    @RequestMapping(value = "/index-06",method = RequestMethod.GET)
    public String index06(){
        return "index-06";
    }
    @RequestMapping(value = "/index-07",method = RequestMethod.GET)
    public String index07(){
        return "index-07";
    }
    @RequestMapping(value = "/index-08",method = RequestMethod.GET)
    public String index08(){
        return "index-08";
    }
    @RequestMapping(value = "/index-09",method = RequestMethod.GET)
    public String index09(){
        return "index-09";
    }
    @RequestMapping(value = "/index-10",method = RequestMethod.GET)
    public String index10(){
        return "index-10";
    }
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public String login(){
        return "login";
    }
    @RequestMapping(value = "/register",method = RequestMethod.GET)
    public String register(){ return "register"; }
    @RequestMapping(value = "/cart",method = RequestMethod.GET)
    public String cart(){
        return "cart";
    }
    @RequestMapping(value = "/checkout",method = RequestMethod.GET)
    public String checkout(){
        return "checkout";
    }
    @RequestMapping(value = "/my-account",method = RequestMethod.GET)
    public String myAccount(Model model){
        List<Orders> orders=new ArrayList<>();
        Orders order=new Orders();
        order.setAddress("M78星云");
        order.setDate(new Date());
        order.setIspay(1);
        order.setTotal(9999.0f);
        order.setLocation("地球");
        order.setTel("123456789");

        orders.add(order);
        orders.add(order);
        orders.add(order);
        orders.add(order);
        orders.add(order);

        model.addAttribute("orders",orders);

        Customer customer=new Customer();
        customer.setUname("马云");
        customer.setTel("123456");
        customer.setEmail("5689@qq.com");
        customer.setAddress("M79星云");
        model.addAttribute("customer",customer);
        return "my-account";
    }

    /**
     * 展示所有商品种类
     * @param model
     * @return
     */
    @RequestMapping(value = "/shop-sidebar",method = RequestMethod.GET)
    public String shopSidebar(Model model){
        //List<Cata> cataList= supermarketService.cataList();
        List<Cata> cataList=new ArrayList<>();
        Cata c=new Cata();
        c.setName("clothes");
        c.setTnum(50);
        cataList.add(c);
        Cata c2=new Cata();
        c2.setName("shoes");
        c2.setTnum(40);
        cataList.add(c2);

        model.addAttribute("cataList",cataList);


        PageInfo pageInfo = shopService.getCataGoods2(0,1,8);
        model.addAttribute("pager",pageInfo);
        return "shop-sidebar";
    }
    @RequestMapping("/search")
    public String search(String search,Model model){
        PageInfo pageInfo = supermarketService.searchGoods(search, 1, 8);
        model.addAttribute("pager",pageInfo);
        return "shop-sidebar";
    }

    /**
     * customer退出登录
     * @return 重定向到登录页面
     */
    @RequestMapping("/logout")
    public String logout(){
        //
        return "redirect:login";
    }
}
